package com.zzq.calcgame.model;

import android.content.Context;
import android.text.TextUtils;

import com.zzq.calcgame.entity.Operator;
import com.zzq.calcgame.entity.Papers;
import com.zzq.calcgame.entity.Question;
import com.zzq.calcgame.storage.PapersDao;
import com.zzq.calcgame.storage.QuestionDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/26.
 */
public class ExamManager {
    private  static ExamManager manager;
    Context context;
    PapersDao papersDao;
    QuestionDao questionDao;

    public static ExamManager getInstance(Context context){
        if(manager==null){
            manager=new ExamManager(context.getApplicationContext());
        }
        return manager;
    }

    public ExamManager(Context context) {
        this.context = context;
        papersDao=new PapersDao(context);
        questionDao=new QuestionDao(context);
    }

    public List<Papers> getAllPaper(){
        return papersDao.getAll();
    }

    /**
     * 创建考试题目
     * @param questionCount 题目总数
     * @return
     */
    public List<Question> cratePaper(int questionCount){
        List<Question> questions=new ArrayList<>();
        for(int i=0;i<questionCount;i++){
            Question item=new Question();
            item.setOperator(Operator.getOperator(randomNumber(4).intValue()));
            item.setNumber1(String.valueOf(randomNumber(20).intValue()));
            item.setNumber2(item.getOperator()==Operator.DIVISION?String.valueOf(randomNumber(20).intValue()+1):String.valueOf(randomNumber(10).intValue()));
            questions.add(item);
        }
        return questions;
    }

    public List<Question> getQuestionByPaperId(int paperId){
        return questionDao.getQuestionByPaperId(paperId);
    }

    /**
     * 交卷 保存数据
     * @param questions
     * @return
     */
    public boolean submit(ArrayList<Question> questions){
        boolean result=false;
        if(questions==null||questions.isEmpty()){
            return result;
        }else{
            Papers papers=new Papers();
            papers.setCreateTime(System.currentTimeMillis());
            papers.setQuestionCount(questions.size());
            int paperId;
            if((paperId=papersDao.insert(papers))!=-1){
                Question item;
                int right=0;//正确个数
                int num1,num2,answer;
                for(int i=0;i<questions.size();i++){
                    item=questions.get(i);
                    answer = Integer.parseInt(item.getAnswer());
                    num1 = Integer.parseInt(item.getNumber1());
                    num2 = Integer.parseInt(item.getNumber2());
                    if((item.getOperator()==Operator.ADD&&answer == num1 + num2)
                            ||(item.getOperator()==Operator.ADD&&answer==num1-num2)
                            ||(item.getOperator()==Operator.TIMES&&answer==num1*num2)
                            ||(item.getOperator()==Operator.DIVISION&&answer==num1/num2)){
                        right++;
                    }
                    questions.get(i).setPaperId(paperId);
                    if(TextUtils.isEmpty(questions.get(i).getAnswer())){
                        questions.get(i).setAnswer("0");
                    }
                }
                papers.setTotalScore(right);
                papers.setWrongCount(questions.size()-right);
            }
            papers.setId(paperId);
            result=papersDao.update(papers)!=-1&&questionDao.batchInsert(questions);
        }
        return result;
    }

    private Double randomNumber(int maxValue){
        return Math.random()*maxValue;
    }

}
